'use strict';

// 有道翻译
const puppeteer = require('puppeteer');

function fy(text) {
    text = encodeURIComponent(text);
    const baseUrl = 'https://fanyi.youdao.com/index.html';
    return new Promise((reslove) => {
        (async () => {
            const browser = await puppeteer.launch();
            const page = await browser.newPage();
            await page.goto(baseUrl);
            await page.setViewport({width: 1080, height: 1024});
            await new Promise((r) => setTimeout(r, 2000));
            const jsFyInput = await page.$('#js_fanyi_input');
            const res = [];
            if (jsFyInput) {
                jsFyInput.type( '国');
                await new Promise((r) => setTimeout(r, 2000));
                const jsFyOutput = await page.$('#js_fanyi_output_resultOutput');
                res.push(jsFyOutput.toString());
            }
            reslove(res);
        })();
    });
}

const args = process.argv;
if (args.length === 3) {
    fy(args[2]).then((res) => {
        console.log(JSON.stringify(res));
    });
}
